package net.lastcoder.ifserver.web;

import net.lastcoder.ifserver.Globals;
import net.lastcoder.ifserver.module.BaseModule;
import net.lastcoder.ifserver.module.ModuleNames;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * auth: WenYF
 * date: 2017/3/1
 */
public class WebManager extends BaseModule {
    private static final Logger Log = LoggerFactory.getLogger(WebManager.class);

    private static class SingletonHolder {
        private static final WebManager instance = new WebManager();
    }

    public static WebManager getInstance() {
        return SingletonHolder.instance;
    }

    private HashMap<String, AbstractHandler> outHandlers;

    private Server server;
    private WebManager() {
        super(ModuleNames.ServletManager);
        outHandlers = new HashMap<>();
    }

    @Override
    public void init() {
        startServer();
    }

    @Override
    public void reload() {
        stopServer();
        init();
    }

    @Override
    public void destroy() {
        stopServer();
    }

    private void stopServer() {
        try {
            if (server != null) {
                server.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addHandler(String path, AbstractHandler handler) {
        outHandlers.put(path, handler);
    }

    public void removeHandler(String path) {
        outHandlers.remove(path);
    }

    public HashMap<String, AbstractHandler> getHandlers() {
        return outHandlers;
    }

    public AbstractHandler getHandler(String path) {
        return outHandlers.get(path);
    }

    private void startServer() {
        // 是否打开websocket
        if (Globals.getXMLProperty("web.socket.enable", true)) {
            LocalWebSocketHandler handler = new LocalWebSocketHandler();
            server = new Server(Globals.getXMLProperty("web.socket.port", 10500));
            server.setHandler(handler);
            try {
                server.start();
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        }

        // 是否打开http
        if (Globals.getXMLProperty("web.handle.enable", true)) {
            BaseHttpHandler handler = new BaseHttpHandler(this);

            server = new Server(Globals.getXMLProperty("web.handle.port", 10501));
            server.setHandler(handler);
            try {
                server.start();
            } catch (Exception e) {
                Log.error(e.getMessage(), e);
            }
        }
    }
}
